import { defineStore } from 'pinia'
import { ref } from 'vue'
import { loginByUsername, loginByPhone } from "@/apis/login"
import { useCardStore } from '@/stores/card'

export const userUserStore = defineStore('user',() => {
    const userInfo = ref({})
    
    const getUserInfoByPassword = async (form) => {
        const res = await loginByUsername(form)
        userInfo.value = res.data
        const cardStore = useCardStore()
        cardStore.mergeCart()
    }

    const getUserInfoByPhone = async (form) => {
        const res = await loginByPhone(form)
        userInfo.value = res.data
        const cardStore = useCardStore()
        cardStore.mergeCart()
    }

    const clearUserInfo = () => {
        const cardStore = useCardStore()
        cardStore.clearCart()
        userInfo.value = {}
    }

    return {
        userInfo,
        getUserInfoByPassword,
        getUserInfoByPhone,
        clearUserInfo
    }
},
{
  persist: true,
}
)